/*********************************************************************************
  *FileName:  bdc.h
  *Version:   V1.0
  *Date:      2023-09-23
**********************************************************************************/

#ifndef  __bdc_H__
#define  __bdc_H__

/***************************************Includes***********************************/
#include "nsuc16xx_conf.h"
#include "pid.h"

/***************************************Macros***********************************/

#define EPWM_CLKSOURCE_32M						0	
#define EPWM_CLKSOURCE_16M						1	
#define EPWM_CLKSOURCE_8M							2

#define	EPWM_CLK_SOURCE							EPWM_CLKSOURCE_16M			

#if EPWM_CLK_SOURCE == EPWM_CLKSOURCE_32M
#define EPWM_CLK_FREQ 							SYSCLK_FREQ_32MHz
#elseif EPWM_CLK_SOURCE == EPWM_CLKSOURCE_16M
#define EPWM_CLK_FREQ 							(SYSCLK_FREQ_32MHz/2)
#elseif EPWM_CLK_SOURCE == EPWM_CLKSOURCE_8M
#define EPWM_CLK_FREQ 							(SYSCLK_FREQ_32MHz/4)
#else
#define EPWM_CLK_FREQ 							(SYSCLK_FREQ_32MHz)
#endif


#define	EPWM_COUNTER_MODE						EPWM_Counter_EdgeAlign
#define	EPWM_PWM_FREQ							16000U
#define	EPWM_PERIOD								(uint16_t)(EPWM_CLK_FREQ/EPWM_PWM_FREQ/(1+EPWM_Counter_EdgeAlign))	

#define POSITION_MAX							10000 //100%
#define POSITION_MIN							0

#define RESISTANCE_MAX_NORMAL					13147    //166D
#define RESISTANCE_MIN_NORMAL					3000		//79D
#define RESISTANCE_MAX_TEST_MODE				4485 
#define RESISTANCE_MIN_TEST_MODE				3000
#define RESISTANCE_MAX_OFFSET					320
#define RESISTANCE_MIN_OFFSET					320


#define POSITION_DEAD_STALL						227U
#define POSITION_DEAD							(int16_t)227




#define POSITION_PID_PERIOD 1U /* Millisecond */
#define POSITION_PID_P 8U		 /* Proportion */
#define POSITION_PID_I 1U		 /* Integral */
#define POSITION_PID_D 0U		 /* Differential */
#define POSITION_PID_EERORSUM 0U
#define POSITION_PID_EERORSUM_MAX 8000U
#define POSITION_PID_INTEGRAL 0	/* Integral initial value */
#define POSITION_PID_LAST_ERROR 0 /* Last error */

#define POSITION_PID_DEAD								(int16_t)15
#define POSITION_PID_PWM_DUTY_MAX (uint16_t)(EPWM_PERIOD*0.9) 							
#define POSITION_PID_PWM_DUTY_MIN (uint16_t)(EPWM_PERIOD*0.4) 
#define POSITION_CALIMODE_PWM_DUTY (uint16_t)(EPWM_PERIOD*0.61) 
#define POSITION_TESTMODE_PWM_DUTY (uint16_t)(EPWM_PERIOD*0.5) 

#define HARD_CURRENT_LIMIT_MA (200*2) //线缆电流:350mA  电机相电流:760mA  ADC采集电流:780-810mA  
#define HARD_CURRENT_LIMIT_SET (HARD_CURRENT_LIMIT_MA*256/1400)
/***************************************Variables***********************************/


typedef enum {
    ROTOR_DIR_UNDEF,
    ROTOR_DIR_CW,
    ROTOR_DIR_CCW,
} Rotor_Direction;

typedef enum
{
	MOTOR_STOP = 0U,
	MOTOR_DIR_CW,
	MOTOR_DIR_CCW,
	MOTOR_DIR_NR_OF
} MotorDir_t;

typedef enum
{
	ACTUATOR_MODE_NORMAL = 0,
	ACTUATOR_MODE_SOFT,
	ACTUATOR_MODE_SHUTDOWN,
	ACTUATOR_MODE_NR_OF
} ActuatorMode_t;

typedef enum
{
	ACTUATOR_STATUS_UNKOWN = 0,
	ACTUATOR_STATUS_CALIBRATION,
	ACTUATOR_STATUS_KOWN,
	ACTUATOR_NR_OF
} ActuatorStatus_t;

typedef enum
{
	MOTOR_NO_FAULT = 0U,
	MOTOR_COIL_SHORT,
	MOTOR_COIL_OPEN,
	MOTOR_FAULT_NR_OF
} MotorFault_t;

typedef enum
{
	MOTOR_REQUEST_NONE = 0,                                                   /**< 0: No request */
	MOTOR_REQUEST_STOP,                                                       /**< Request to STOP motor */
	MOTOR_REQUEST_START,                                                      /**< Request to START/SET motor */
	MOTOR_REQUEST_CALIBRATION_CCW,                                            /**< Request to start calibration */
	MOTOR_REQUEST_CALIBRATION_CW,                                             /**< Request to start calibration */
	MOTOR_REQUEST_SLEEP,                                                      /**< Request to sleep */
} MotorRequest_t;

typedef enum
{
	MOTOR_STATUS_STOP = 0,                                                    /**< 0: Motor Stop */
	MOTOR_STATUS_RUNNING,                                                     /**< Motor Runing */
	MOTOR_STATUS_STOPPING,																										/**< Motor Stoping */
	MOTOR_STATUS_DEGRADED,                                                    /**< Motor Degraded */
} MotorStatus_t;

typedef enum
{
	ERR_VOLTAGE_IN_RANGE = 0U,  																							/**< Module within operational voltage range */
	ERR_VOLTAGE_OVER,          	 																							/**< Over software voltage threshold */
	ERR_VOLTAGE_UNDER,          																							/**< Under software voltage threshold */
	ERR_VOLTAGE_NR_OF
} ErrVoltage_t;

typedef enum
{
	ERR_TEMP_NO = 0U,  																												/**< Module within operational temperature range */
	ERR_TEMP_WARNING,  																												/**< Temperature warning */
	ERR_TEMP_ALARM,    																												/**< Temperature alarm */
	ERR_TEMP_UNDER,    																												/**< Temperature under */
	ERR_TEMP_NR_OF
} ErrTemp_t;

typedef enum
{
	ERR_STALL_NOFOUND = 0U,																										/**< Motor Stall no found */
	ERR_STALL_FOUND,																													/**< Motor Stall found */
	ERR_STALL_NR_OF
} MotorStall_t;

typedef struct{
	/* motor current parameter */
	uint16_t u16Duty;																													/* motor pwm duty  */
	uint16_t u16Current;
	int16_t Motor_Current_Offect;
	MotorStatus_t RunStatus;																									/* motor run status */
	MotorRequest_t Request;																										/* motor request type */
	MotorDir_t Direction;																											/* motor run direction */
	MotorStall_t StallState;																									/* motor stall status */
	MotorFault_t Fault;																												/* motor fault status */
	uint16_t u16MotorRunTimer;
} BDCMotor_Parameter_t;

typedef struct{
	
	uint16_t u16ActualPosition;		/* actual position */
	uint16_t u16TargetPosition;		/* target position */
	uint16_t u16TargetPosition_Last;		/* target position */
	
	uint16_t u16ResistanceVoltage;
	uint16_t u16ResistanceSupplyVoltage;
	
	uint16_t u16SupplyVoltage;		/* supply voltage */
	int16_t i16Temperature;			/* temperature */
	
	BDCMotor_Parameter_t Motor;	/* step motor parameter */
	ActuatorMode_t Mode;			/* actuator mode */
	ActuatorStatus_t PositionStatus;/* actuator position status*/
	ErrVoltage_t ErrorVoltage;		/* actuator Voltage status*/
	ErrTemp_t ErrorTemperature;		/* actuator temperature status*/
	uint16_t u16Stop2StartDelay;	/* actuator stop to start delay time*/
}Actuator_Parameter_t;




extern Actuator_Parameter_t Actuator;
extern FILTER_PID_Object_t l_sPositionPidObject;
extern uint8_t g_u8OverCurrent_Cnt;
/***************************************Functions***********************************/
void MotorDriver_Init(void);
void MotorDriverStart(void);
void MotorDriverStop(void);
void PID_Control(void);
void MotorDriverTristate(void);
void MotorDriverEnable(void);
MotorFault_t MotorOpenLoadCherck(uint16_t u16Current);
void Motor_PWM_Set(MotorDir_t MotorDir, uint16_t u16DutyCycle);

#endif
/* [] END OF bdc.h */
